﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BCDataCenter.BOL;

public partial class WebUserControl_PhanQuyenUser : System.Web.UI.UserControl
{
    string error = "";
    static DataTable dtQanTriHT, dtXNHang, dtDuLieuNen, dtBaoCao, dtInCT, dtChotNV, dtLietKeDL, dtTienIch ;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadUser();
            LoadMenuCatalogue();
        }
    }
    private void LoadUser()
    {
        BWMenuCatalogueUser obj = new BWMenuCatalogueUser();
        DropDownListUser.DataSource = obj.dsUserList(ref error);
        DropDownListUser.DataTextField = "UserAndType";
        DropDownListUser.DataValueField = "UserName";
        DropDownListUser.DataBind();
    }
    private void LoadMenuCatalogue()
    {
        string UserName = DropDownListUser.SelectedItem.Value;
        BWMenuCatalogueUser obj = new BWMenuCatalogueUser();
        dtBaoCao = new DataTable();
        dtChotNV = new DataTable();
        dtDuLieuNen = new DataTable();
        dtInCT = new DataTable();
        dtLietKeDL = new DataTable();
        dtQanTriHT = new DataTable();
        dtTienIch = new DataTable();
        dtXNHang = new DataTable();

        dtQanTriHT = obj.dsMenuCatalogue(ref error, "QTHT", UserName).Tables[0];
        gvQuanTriHeThong.DataSource = dtQanTriHT;
        
        dtXNHang = obj.dsMenuCatalogue(ref error, "XNH", UserName).Tables[0];
        gvXuatNhapHang.DataSource = dtXNHang;

        dtDuLieuNen = obj.dsMenuCatalogue(ref error, "DLN", UserName).Tables[0];
        gvDuLieuNen.DataSource = dtDuLieuNen;
        
        dtBaoCao = obj.dsMenuCatalogue(ref error, "BC", UserName).Tables[0];
        gvBaoCao.DataSource = dtBaoCao;

        dtInCT = obj.dsMenuCatalogue(ref error, "ICT", UserName).Tables[0];
        gvInChungTu.DataSource = dtInCT;

        dtChotNV = obj.dsMenuCatalogue(ref error, "CNV", UserName).Tables[0];
        gvChotNghiepVu.DataSource = dtChotNV;

        dtLietKeDL = obj.dsMenuCatalogue(ref error, "LKDL", UserName).Tables[0];
        gvLietKeDuLieu.DataSource = dtLietKeDL;

        dtTienIch = obj.dsMenuCatalogue(ref error, "TI", UserName).Tables[0];
        gvTienIch.DataSource = dtTienIch;
        
        gvQuanTriHeThong.DataBind();
        gvXuatNhapHang.DataBind();
        gvDuLieuNen.DataBind();
        gvBaoCao.DataBind();
        gvInChungTu.DataBind();
        gvChotNghiepVu.DataBind();
        gvLietKeDuLieu.DataBind();
        gvTienIch.DataBind();

    }

    private DataTable create_TableUserModule()
    {
        DataTable dtUserModule = new DataTable();
        DataColumn dtc;

        //Tao Cot UserName
        dtc = new DataColumn();
        dtc.DataType = Type.GetType("System.String");
        dtc.ColumnName = "UserName";
        dtUserModule.Columns.Add(dtc);

        //Tao Cot PageID
        dtc = new DataColumn();
        dtc.DataType = Type.GetType("System.String");
        dtc.ColumnName = "PageID";
        dtUserModule.Columns.Add(dtc);

        //Tao Cot PageName
        dtc = new DataColumn();
        dtc.DataType = Type.GetType("System.String");
        dtc.ColumnName = "PageName";
        dtUserModule.Columns.Add(dtc);
        //Tao Cot MenuID
        dtc = new DataColumn();
        dtc.DataType = Type.GetType("System.String");
        dtc.ColumnName = "MenuID";
        dtUserModule.Columns.Add(dtc);

        //Tao Cot Count
        dtc = new DataColumn();
        dtc.DataType = Type.GetType("System.String");
        dtc.ColumnName = "Count";
        dtUserModule.Columns.Add(dtc);

        return dtUserModule;
    }
  
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (DropDownListUser.SelectedIndex != -1)
        {
            try
            {
                DataTable dt = create_TableUserModule();
                
                string UserName = DropDownListUser.SelectedItem.Value;
                for (int i = 0; i < gvQuanTriHeThong.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvQuanTriHeThong.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtQanTriHT.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtQanTriHT.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtQanTriHT.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvXuatNhapHang.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvXuatNhapHang.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtXNHang.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtXNHang.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtXNHang.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvDuLieuNen.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvDuLieuNen.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtDuLieuNen.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtDuLieuNen.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtDuLieuNen.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvBaoCao.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvBaoCao.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtBaoCao.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtBaoCao.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtBaoCao.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvInChungTu.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvInChungTu.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtInCT.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtInCT.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtInCT.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvChotNghiepVu.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvChotNghiepVu.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtChotNV.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtChotNV.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtChotNV.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvLietKeDuLieu.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvLietKeDuLieu.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtLietKeDL.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtLietKeDL.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtLietKeDL.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }
                for (int i = 0; i < gvTienIch.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    GridViewRow row = gvTienIch.Rows[i];
                    bool Ischeck = ((CheckBox)row.FindControl("checkPage")).Checked;
                    if (Ischeck)
                    {
                        dr["UserName"] = UserName;
                        dr["PageID"] = dtTienIch.Rows[i]["PageID"].ToString();
                        dr["PageName"] = dtTienIch.Rows[i]["PageName"].ToString();
                        dr["MenuID"] = dtTienIch.Rows[i]["MenuID"].ToString();
                        dr["Count"] = dt.Rows.Count.ToString();
                        dt.Rows.Add(dr);
                    }
                }


                if (new BWMenuCatalogueUser().InsertUserModule(ref error, dt))
                {
                    //bind phân quyền vừa cập nhật vào session
                    ClassExecute.ClassExecute objd = new ClassExecute.ClassExecute();
                    DataTable db = objd.Get_Data_DataTable(str, ref error, "[k_getPageUser]", CommandType.StoredProcedure,
                             new SqlParameter("@UserName", Session["username"].ToString()));
                    Session["PageUrl"] = null;
                    Session["PageUrl"] = db;

                    uscMsgBox1.AddMessage("Lưu phân quyền cho người dùng [" + DropDownListUser.SelectedItem.Text + "] thành công.", uscMsgBox.enmMessageType.Success);
                   
                }
                else
                {
                    uscMsgBox1.AddMessage("Lưu phân quyền cho người dùng [" + DropDownListUser.SelectedItem.Text + "] thất bại.\n" + error, uscMsgBox.enmMessageType.Error);
                }

            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(error + "\n" + ex.Message, uscMsgBox.enmMessageType.Error);
                return;
            }
        }
        else
        {
            uscMsgBox1.AddMessage("Chưa chọn user cần phân quyền'", uscMsgBox.enmMessageType.Error);
        }
    }
    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    
    protected void checkAllQTHT_CheckedChanged(object sender, EventArgs e)
    {
        bool Ischeck = ((CheckBox)gvQuanTriHeThong.FooterRow.FindControl("checkAll")).Checked;
        if (Ischeck)
        {
            for (int i = 0; i < gvQuanTriHeThong.Rows.Count; i++)
            {
                CheckBox checkboxgv = (CheckBox)gvTienIch.Rows[i].FindControl("checkPage");
                checkboxgv.Checked = true;
                
            }
        }
        else
        {
            for (int i = 0; i < gvQuanTriHeThong.Rows.Count; i++)
            {
                CheckBox checkboxgv = (CheckBox)gvTienIch.Rows[i].FindControl("checkPage");
                checkboxgv.Checked = false;
            }
        }
    }
    protected void checkAllXNHang_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void checkAllDLNen_CheckedChanged(object sender, EventArgs e)
    {
        
    }
    protected void checkAllBaCao_CheckedChanged(object sender, EventArgs e)
    {
        
    }
    protected void checkAllInCT_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void checkAllChotNV_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void checkAllLietKDL_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void checkAllTienIch_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownListUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadMenuCatalogue();
    }
}
